package UIFree.FX.pageturner
{
	import UIFree.uif_ecomponent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	
	public class pt_3DTurner extends pt_base3DTurner
	{
		private var _particles: uif_ecomponent;
		private var _plist: Array;
		
		private var xCount: uint;
		private var yCount: uint;
		
		public function pt_3DTurner(parent:uif_ecomponent, a_transition:Function=null, a_fps:Number=20, a_duration:Number=1500)
		{
			super(parent, a_transition, a_fps, a_duration);
			_particles = new uif_ecomponent(this);
			_plist = new Array();
			xCount = 4;
			yCount = 4;			
		}
		
		
		protected function createParticles(): void {
			var pSize: Point = new Point(startBitmap.bitmapData.width / xCount, startBitmap.bitmapData.height / yCount);
			for (var iy: int=yCount-1; iy>=0; iy--)
				for (var ix: int=xCount-1; ix>=0; ix--) {
					var s1: Bitmap = new Bitmap(new BitmapData(pSize.x, pSize.y, false));
					var s2: Bitmap = new Bitmap(new BitmapData(pSize.x, pSize.y, false));
					
					var mat: Matrix = new Matrix();
					mat.translate(-ix * pSize.x, -iy * pSize.y);
					s1.bitmapData.draw(startBitmap, mat);
					s2.bitmapData.draw(endBitmap, mat);
					
					_particles.addChild(s1);
					_particles.addChild(s2);
					var obj: Object = {
						side1: s1,
						side2: s2, 
						roll: new Vector3D(0, 1, 1),//new Vector3D(Math.random(), Math.random(), Math.random()),
						rCount: Math.ceil(Math.random() * 3), 
							depth: (Math.random() - 0.5) * 800,
							pos: new Point(ix * pSize.x, iy * pSize.y)};
					(obj.roll as Vector3D).normalize(); 
					_plist.push(obj);
				}
		}
		
		override protected function startFX(): void {
			createParticles();
		}
		
		override protected function updateFromState(): void {
			super.updateFromState();
			
			var pcenter: Point = new Point(startBitmap.bitmapData.width / xCount / 2, startBitmap.bitmapData.height / yCount / 2);
			var sine: Number = Math.sin(state * Math.PI);
			var mat: Matrix3D = new Matrix3D();
			var center: Point = new Point(startBitmap.width / 2, startBitmap.height / 2);
			mat.appendTranslation(-center.x, -center.y, 0);
			//			mat.appendRotation(state * 180, new Vector3D(1, 0, 0)); 
			mat.appendTranslation(center.x, center.y, sine * 500); 
			_particles.transform.matrix3D = mat;
			
			var i: uint=0;
			for (var iy: int=0; iy<yCount; iy++)
				for (var ix: int=0; ix<xCount; ix++) {
					mat = new Matrix3D();
					mat.appendTranslation(-pcenter.x, -pcenter.y, 0);
					mat.appendRotation(state * 360 * _plist[i].rCount, _plist[i].roll);
					mat.appendTranslation(pcenter.x + _plist[i].pos.x, pcenter.y + _plist[i].pos.y, sine * _plist[i].depth);
					_plist[i].side1.transform.matrix3D = mat;
					_plist[i].side2.transform.matrix3D = mat.clone();
					
					_plist[i].side1.alpha = 1 - state;
					_plist[i].side2.alpha = state;
					/*					particle.z = (Math.sin(iy / yCount * Math.PI * 3) +
					Math.cos(ix / xCount * Math.PI * 3)) * 200 * sine;*/
					i++;
				}
		}
		
		override protected function endFX(): void {
			_particles.removeChilds();
			_plist.splice(0, _plist.length);
		}		
	}
}